package com.da.javatest.niuke.jianzhi;

import java.util.Stack;

/**
 * @author chenlida
 * @date 2020/10/21 19:03
 * https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?
 * tpId=13&tags=&title=&diffculty=0&judgeStatus=0&rp=1
 */
public class C21Solution {
    public static boolean IsPopOrder(int[] pushA, int[] popA) {
        Stack<Integer> stack = new Stack<>();
        int maxIndex = popA.length - 1;
        int index = 0;
        for (int val : pushA) {
            stack.push(val);
            while (!stack.empty() && index <= maxIndex && popA[index] == stack.peek()) {
                index++;
                stack.pop();
            }
        }

        return stack.empty();
    }

    public static void main(String[] args) {
        System.out.println(IsPopOrder(new int[] {1, 2, 3, 4, 5}, new int[] {4, 5, 3, 2, 1}));
        System.out.println(IsPopOrder(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}));
        System.out.println(IsPopOrder(new int[] {1, 2, 3, 4, 5}, new int[] {4, 3, 5, 1, 2}));
    }
}
